﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace EAIClassLibrary
{
    public class ESBEnvelopeEntity
    {
        public string ESBHdVer { get; set; }
        public string SrvDate { get; set; }
        public string SrvTime { get; set; }
        public string SenderID { get; set; }
        public string ReceiverID { get; set; }
        public string SrvOpName { get; set; }
        public string SrvOpVer { get; set; }
        public string MsgID { get; set; }
        public string CorrID { get; set; }
        public string ESBRspCode { get; set; }
        public string ESBRspDec { get; set; }
        public string ResField1 { get; set; }
        public string ResField2 { get; set; }
        public string ResField3 { get; set; }
        public string ResField4 { get; set; }
        public string ResField5 { get; set; }

        public string MsgBody { get; set; }

        /// <summary>
        /// Create entity from xml.
        /// </summary>
        /// <param name="xml"></param>
        public static ESBEnvelopeEntity FromXML(XmlDocument sourceXml)
        {
            ESBEnvelopeEntity entity = new ESBEnvelopeEntity();

            var esbEnvolope = sourceXml["ESBEnvelope", "http://MetLifeEAI.EAISchema"];
            for (int j = 0; j < esbEnvolope.ChildNodes.Count; j++)
            {
                var esbItem = esbEnvolope.ChildNodes[j];

                //ESB Header
                if (esbItem.Name == "ESBHeader")
                {
                    for (int i = 0; i < esbItem.ChildNodes.Count; i++)
                    {
                        var node = esbItem.ChildNodes[i];

                        if (node.Name == "ESBHdVer") { entity.ESBHdVer = node.InnerXml; }
                        if (node.Name == "SrvDate") { entity.SrvDate = node.InnerXml; }
                        if (node.Name == "SrvTime") { entity.SrvTime = node.InnerXml; }
                        if (node.Name == "SenderID") { entity.SenderID = node.InnerXml; }
                        if (node.Name == "ReceiverID") { entity.ReceiverID = node.InnerXml; }
                        if (node.Name == "SrvOpName") { entity.SrvOpName = node.InnerXml; }
                        if (node.Name == "SrvOpVer") { entity.SrvOpVer = node.InnerXml; }
                        if (node.Name == "MsgID") { entity.MsgID = node.InnerXml; }
                        if (node.Name == "CorrID") { entity.CorrID = node.InnerXml; }
                        if (node.Name == "ESBRspCode") { entity.ESBRspCode = node.InnerXml; }
                        if (node.Name == "ESBRspDec") { entity.ESBRspDec = node.InnerXml; }
                        if (node.Name == "ResField1") { entity.ResField1 = node.InnerXml; }
                        if (node.Name == "ResField2") { entity.ResField2 = node.InnerXml; }
                        if (node.Name == "ResField3") { entity.ResField3 = node.InnerXml; }
                        if (node.Name == "ResField4") { entity.ResField4 = node.InnerXml; }
                        if (node.Name == "ResField5") { entity.ResField5 = node.InnerXml; }
                    }
                }

                if (esbItem.Name == "MsgBody")
                {
                    //ESB Body
                    entity.MsgBody = esbItem.InnerXml;
                }
            }
            return entity;
        }

        /// <summary>
        /// Create XmlDocument from entity.
        /// </summary>
        /// <returns></returns>
        public XmlDocument ToXml()
        {
            var str = string.Format("<ns0:ESBEnvelope xmlns:ns0=\"http://MetLifeEAI.EAISchema\"><ESBHeader><ESBHdVer>{0}</ESBHdVer><SrvDate>{1}</SrvDate><SrvTime>{2}</SrvTime>"
                + "<SenderID>{3}</SenderID><ReceiverID>{4}</ReceiverID><SrvOpName>{5}</SrvOpName>"
            + "<SrvOpVer>{6}</SrvOpVer><MsgID>{7}</MsgID><CorrID>{8}</CorrID><ESBRspCode>{9}</ESBRspCode><ESBRspDec>{10}</ESBRspDec>"
            + "<ResField1>{11}</ResField1><ResField2>{12}</ResField2><ResField3>{13}</ResField3><ResField4>{14}</ResField4><ResField5>{15}</ResField5></ESBHeader><MsgBody>{16}</MsgBody></ns0:ESBEnvelope>",
                ESBHdVer, SrvDate, SrvTime,
                SenderID, ReceiverID, SrvOpName,
                SrvOpVer, MsgID, CorrID, ESBRspCode, ESBRspDec,
                ResField1, ResField2, ResField3, ResField4, ResField5, MsgBody);

            XmlDocument document = new XmlDocument();
            document.LoadXml(str);
            //document.LoadXml("<ns0:ESBEnvelope xmlns:ns0=\"http://MetLifeEAI.EAISchema\"><ESBHeader><ESBHdVer>20120608_1.1</ESBHdVer><SrvDate>2012/09/08</SrvDate><SrvTime>11:19:39</SrvTime><SenderID>SenderID_0</SenderID><ReceiverID>ReceiverID_0</ReceiverID><SrvOpName>SrvOpName_0</SrvOpName><SrvOpVer>20120606_1.1</SrvOpVer><MsgID>3be00f45-945b-44d9-b3ef-8824bca5ff14</MsgID><CorrID>5e99dd10-3237-4cfb-8657-6c3251306dd8</CorrID><ESBRspCode>0</ESBRspCode><ESBRspDec>Success</ESBRspDec><ResField1>ResField1_0</ResField1><ResField2>ResField2_0</ResField2><ResField3>ResField3_0</ResField3><ResField4>ResField4_0</ResField4><ResField5>ResField5_0</ResField5></ESBHeader><MsgBody><Sample>I'm from EAI.</Sample></MsgBody></ns0:ESBEnvelope>");

            return document;
        }

        /// <summary>
        /// Create result XmlDocument from entity.
        /// </summary>
        /// <returns></returns>
        public XmlDocument ToResultXml()
        {
            var str = string.Format("<ns0:ESBEnvelopeResult xmlns:ns0=\"http://MetLifeEAI.EAISchema\"><ESBHeader><ESBHdVer>{0}</ESBHdVer><SrvDate>{1}</SrvDate><SrvTime>{2}</SrvTime>"
                + "<SenderID>{3}</SenderID><ReceiverID>{4}</ReceiverID><SrvOpName>{5}</SrvOpName>"
            + "<SrvOpVer>{6}</SrvOpVer><MsgID>{7}</MsgID><CorrID>{8}</CorrID><ESBRspCode>{9}</ESBRspCode><ESBRspDec>{10}</ESBRspDec>"
            + "<ResField1>{11}</ResField1><ResField2>{12}</ResField2><ResField3>{13}</ResField3><ResField4>{14}</ResField4><ResField5>{15}</ResField5></ESBHeader><MsgBody>{16}</MsgBody></ns0:ESBEnvelopeResult>",
                ESBHdVer, SrvDate, SrvTime,
                SenderID, ReceiverID, SrvOpName,
                SrvOpVer, MsgID, CorrID, ESBRspCode, ESBRspDec,
                ResField1, ResField2, ResField3, ResField4, ResField5, MsgBody);

            XmlDocument document = new XmlDocument();
            document.LoadXml(str);
            //document.LoadXml("<ns0:ESBEnvelope xmlns:ns0=\"http://MetLifeEAI.EAISchema\"><ESBHeader><ESBHdVer>20120608_1.1</ESBHdVer><SrvDate>2012/09/08</SrvDate><SrvTime>11:19:39</SrvTime><SenderID>SenderID_0</SenderID><ReceiverID>ReceiverID_0</ReceiverID><SrvOpName>SrvOpName_0</SrvOpName><SrvOpVer>20120606_1.1</SrvOpVer><MsgID>3be00f45-945b-44d9-b3ef-8824bca5ff14</MsgID><CorrID>5e99dd10-3237-4cfb-8657-6c3251306dd8</CorrID><ESBRspCode>0</ESBRspCode><ESBRspDec>Success</ESBRspDec><ResField1>ResField1_0</ResField1><ResField2>ResField2_0</ResField2><ResField3>ResField3_0</ResField3><ResField4>ResField4_0</ResField4><ResField5>ResField5_0</ResField5></ESBHeader><MsgBody><Sample>I'm from EAI.</Sample></MsgBody></ns0:ESBEnvelope>");

            return document;
        }

        /// <summary>
        /// Create a ESBEnvelopeEntity entity.
        /// </summary>
        /// <returns></returns>
        public static ESBEnvelopeEntity CreateWithHeader()
        {
            ESBEnvelopeEntity entity = new ESBEnvelopeEntity();
            entity.ESBHdVer = "20120608_1.1";
            entity.SrvDate = DateTime.Now.ToString("yyyy-MM-dd");
            entity.SrvTime = DateTime.Now.ToString("hh:mm:ss.ms");
            entity.SrvOpVer = "20120606_1.1";
            entity.MsgID = Guid.NewGuid().ToString();
            entity.CorrID = Guid.NewGuid().ToString();

            return entity;
        }
    }
}
